#include "PreApplication.h"
#include "CameraCtrl.h"

CameraCtrl::CameraCtrl(void)
{
	mLeft = 0;
	mUp = 0;
	mFront = 0;
}

CameraCtrl::~CameraCtrl(void)
{
}

bool CameraCtrl::onMouseMove(int x,int y,int deltaX,int deltaY,Dword flags)
{
	if(flags & MB_Right){
		Real yaw = -deltaX * 0.01f;
		Real pitch =  deltaY * 0.01f;
		if(mCamera){
			mCamera->yaw(Radian(yaw));
			mCamera->pitch(Radian(pitch));
			return true;
		}
	}
	else if(flags & MB_Middle){
		Real left = deltaX * 0.1f;
		Real up = deltaY * 0.1f;
		if(mCamera){
			mCamera->moveRelative(Vector3(left,up,0));
			//mCamera->move(Vector3(0,up,0));
		}
	}
	return false;
}

bool CameraCtrl::onKeyDown(InputKey key)
{
	if(key == IK_W){
		mFront = 1;
	}
	else if(key == IK_S){
		mFront = -1;
	}
	else if(key == IK_A){
		mLeft = 1;
	}
	else if(key == IK_D){
		mLeft = -1;
	}
	else if(key == IK_UP){
		mUp = -1;
	}
	else if(key == IK_DOWN){
		mUp = 1;
	}
	else
		return false;
	return true;
}

bool CameraCtrl::onKeyUp(InputKey key)
{
	if(key == IK_W){
		mFront = 0;
	}
	else if(key == IK_S){
		mFront = 0;
	}
	else if(key == IK_A){
		mLeft = 0;
	}
	else if(key == IK_D){
		mLeft = 0;
	}
	else if(key == IK_UP){
		mUp = 0;
	}
	else if(key == IK_DOWN){
		mUp = 0;
	}
	else
		return false;
	return true;
}

void CameraCtrl::frame()
{
	if(mCamera){
		Real delta = 0.1f;
		mCamera->moveRelative(Vector3(-mLeft*delta,-mUp*delta,-mFront*delta));
	}
}